package com.yum.Iterator.List.Vector.Stack;

import com.yum.Iterator.List.Vector.YVector;

import java.util.EmptyStackException;
import java.util.Stack;

public class YStack<E> extends YVector<E> {

    private static final long serialVersionUID = -2462620402762455743L;

    public E push(E item){
        item = add(item)?item:null;
        return item;
    }

    public synchronized E pop(){
        int len = size();
        E result = peek();
        removeElement(len-1);
        return result;
    }

    public synchronized E peek(){
        int len = size();
        if(len < 0)
            throw new EmptyStackException();
        return elementAt(len-1);
    }

    public synchronized int search(Object obj){
        return indexOf(obj);
    }


    @Override
    public boolean isEmpty() {
        return super.isEmpty();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("[");
        for(int i = eleCount-1;i>=0;i--){
            sb.append(eleData[i].toString());
            if(i!=0){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
